﻿using System;
using msBsc.AppLogic.DataAlc;
using System.Data;

namespace msBsc.WebUi.Custom
{
    public partial class wfCustInfoSet : msBsc.Cls.BPCustom
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                BoundBankList();
                showUserInfo();
            }
        }

        private void BoundBankList()
        {
            using (Epm.AppLogic.Framework.CDbAccess oCDbAccess = new Epm.AppLogic.Framework.CDbAccess())
            {
                string strSql = "SELECT * FROM BSE_BANK ";

                DataSet ds = oCDbAccess.QueryBySQL(strSql);
                this.BoundComboBox("dplBANK_CDE", ds, "BANK_NME", "BANK_CDE", "", "", true);
            }
        }

        private void showUserInfo()
        {
            using (msBsc.AppLogic.DataAlc.CBizShpScorePub objpub = new CBizShpScorePub())
            {
                DataSet ds = objpub.DataObj.QueryBySQL(@"
    SELECT 
    A.USER_NME
    ,A.E_MAIL
    ,A.TEL
    ,A.QQ_NO
    ,A.PROMOTION_NME
    ,B.CUST_REAL_NME
    ,B.ALIPAY_MAIL
    ,B.BANK_CDE
    ,B.BANK_ACCOUNT_NO
    ,B.BANK_ACCOUNT_NME
    ,B.LOGO_IMG_URL
    ,B.TRADING_PSW
    FROM SYS_USER  A LEFT OUTER JOIN SHP_CUSTOMER_INFO B ON A.USER_NME=B.USER_NME WHERE A.USER_NME='" + this.oUserInfo.Operator+@"'
    ");
                this.AddPageRetRow("divInputInfo", ds.Tables[0].Rows[0]);
                this.SetContrlValue("dplBANK_CDE", ds.Tables[0].Rows[0]["BANK_CDE"].ToString());
                if (ds.Tables[0].Rows[0]["LOGO_IMG_URL"].ToString() != string.Empty)
                {
                    this.ExecJScript("$('#logo_img_selected').html('<img alt=\"\" src=\"" + ds.Tables[0].Rows[0]["LOGO_IMG_URL"].ToString() + "\" for=\"" + ds.Tables[0].Rows[0]["LOGO_IMG_URL"].ToString() + "\" width=\"100%\" height=\"100%\">');");
                } 
                
                //SetMode("VIEW");
            }
        }

        //修改页面状态
        private void SetMode(string strMode)
        {
            EditMode = strMode; 
            switch (strMode)
            {
                case "EDIT":
                    EnabledLinkBtn("btnEdit", false);
                    EnabledLinkBtn("btnSave", true);
                    SetPanalEditMode("divInputInfo", "EDIT");
                    break; 
                case "VIEW":
                    EnabledLinkBtn("btnEdit", true);
                    EnabledLinkBtn("btnSave", false);
                    SetPanalEditMode("divInputInfo", "VIEW");
                    break;
            }
        }
        private void doSave()
        {

            System.Transactions.TransactionOptions transactionOption = new System.Transactions.TransactionOptions();
            transactionOption.IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted;
            transactionOption.Timeout = new TimeSpan(0, 10, 0);
            using (System.Transactions.TransactionScope ts = new System.Transactions.TransactionScope(System.Transactions.TransactionScopeOption.Required, transactionOption))
            {
                using (msBsc.AppLogic.DataAlc.CBizSysUser objss = new CBizSysUser())
                {
                    DataSet ds1 = objss.SelectById(this.oUserInfo.Operator);
                    if (ds1.Tables[0].Rows.Count > 0)
                    {
                        ds1.Tables[0].Rows[0]["TEL"] = this.GetPostHtValue("TEL");
                        ds1.Tables[0].Rows[0]["QQ_NO"] = this.GetPostHtValue("QQ_NO");
                        //ds1.Tables[0].Rows[0]["PROMOTION_NME"] = this.GetPostHtValue("PROMOTION_NME");
                        objss.Update(ds1);
                    }
                }
                using (msBsc.AppLogic.DataAlc.CBizShpCustomerInfo objss = new CBizShpCustomerInfo())
                {
                    DataSet ds1 = objss.SelectById(this.oUserInfo.Operator);
                    if (ds1.Tables[0].Rows.Count > 0)
                    {
                        ds1.Tables[0].Rows[0]["CUST_REAL_NME"] = this.GetPostHtValue("CUST_REAL_NME");
                        ds1.Tables[0].Rows[0]["ALIPAY_MAIL"] = this.GetPostHtValue("ALIPAY_MAIL");
                        ds1.Tables[0].Rows[0]["BANK_CDE"] = this.GetPostHtValue("BANK_CDE");
                        ds1.Tables[0].Rows[0]["BANK_ACCOUNT_NO"] = this.GetPostHtValue("BANK_ACCOUNT_NO");
                        ds1.Tables[0].Rows[0]["BANK_ACCOUNT_NME"] = this.GetPostHtValue("BANK_ACCOUNT_NME");
                        //ds1.Tables[0].Rows[0]["LOGO_IMG_URL"] = this.GetPostHtValue("LOGO_IMG_URL");

                        if (this.GetPostHtValue("LOGO_IMG_URL") != string.Empty && ds1.Tables[0].Rows[0]["LOGO_IMG_URL"].ToString() != this.GetPostHtValue("LOGO_IMG_URL"))
                        {
                            using (msBsc.AppLogic.DataAlc.CBizSysFile oCBizSysFile = new msBsc.AppLogic.DataAlc.CBizSysFile())
                            {
                                //保存身份证A面
                                ds1.Tables[0].Rows[0]["LOGO_IMG_URL"] = oCBizSysFile.SaveImgToServer(""
                                    , this.oUserInfo.Operator
                                    , "0"
                                    , "LOGO"
                                    , ""
                                    , this.GetPostHtValue("LOGO_IMG_URL")
                                    , ""
                                    , true);
                            }
                        }

                        objss.Update(ds1);
                    }
                }
                ts.Complete();
            }

            this.ExecJScript("layer.msg('已成功保存！',1,{type:1,shade:false});");
        }

        public override void DoAction()
        {
            if (this.strAction == "btnSave")
            {
                doSave();
                //SetMode("VIEW");
            }
            else if (strAction == "btnEdit")//根据传入的回调类型进行处理，这里响应编辑按钮
            {
                SetMode("EDIT");
            }
        }
    }
}